<?php

namespace app\api\controller;

use app\api\BaseController;
use app\model\Shutup as ShutupModel;
use app\model\User as UserModel;
use think\App;

class Shutup extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        //查询列表时允许的字段
        $this->selectList = "*";
        //查询详情时允许的字段
        $this->selectDetail = "*";
        //筛选字段
        $this->searchFilter = [
            "shutup_id" => "=",
            "shutup_user" => "like",
        ];
        $this->insertFields = [
            //允许添加的字段列表
            "shutup_user",
        ];
        $this->updateFields = [
            //允许更新的字段列表
            "shutup_user",
        ];
        $this->insertRequire = [
            //添加时必须填写的字段
            // "字段名称"=>"该字段不能为空"

        ];
        $this->updateRequire = [
            //修改时必须填写的字段
            // "字段名称"=>"该字段不能为空"

        ];
        $this->model = new ShutupModel();
    }

    public function addByUser()
    {
        $error = $this->access();
        if ($error) {
            return $error;
        }
        if (!input('user_id')) {
            return jerr("参数错误");
        }
        $userModel = new UserModel();
        $user = $userModel->where('user_id', input('user_id'))->find();
        if (!$user) {
            return jerr('用户信息查询失败');
        }
        $data = [];
        $data[$this->table . "_user"] = input('user_id');
        $this->model->where($data)->delete();
        $data[$this->table . "_updatetime"] = time();
        $data[$this->table . "_createtime"] = time();
        $this->model->insert($data);

        $msg = json_encode([
            'user' => $this->user['user_id'],
            'type' => 'system',
            'at' => "",
            'msg' => $user['user_name'] . " 被 " . $this->user['user_name'] . " 禁言",
            'extra' => null,
            'time' => date('H:i:s'),
        ]);
        $ret = curlHelper("http://127.0.0.1:9999", "POST", http_build_query([
            'token' => getWebsocketSign(),
            'channel' => getWebsocketChannel(),
            'msg' => $msg,
        ]), [
            'content-type:application/x-www-form-urlencode',
        ]);

        return jok('禁止用户发言成功');
    }
    public function deleteByUser()
    {
        $error = $this->access();
        if ($error) {
            return $error;
        }

        if (!input('user_id')) {
            return jerr("参数错误");
        }

        $userModel = new UserModel();
        $user = $userModel->where('user_id', input('user_id'))->find();
        if (!$user) {
            return jerr('用户信息查询失败');
        }
        $this->model->where($this->table . "_user", input('user_id'))->delete();

        $msg = json_encode([
            'user' => $this->user['user_id'],
            'type' => 'system',
            'at' => "",
            'msg' => $user['user_name'] . " 被 " . $this->user['user_name'] . " 允许发言",
            'extra' => null,
            'time' => date('H:i:s'),
        ]);
        $ret = curlHelper("http://127.0.0.1:9999", "POST", http_build_query([
            'token' => getWebsocketSign(),
            'channel' => getWebsocketChannel(),
            'msg' => $msg,
        ]), [
            'content-type:application/x-www-form-urlencode',
        ]);

        return jok('允许用户发言成功');
    }
}
